//
// Created by Administrator on 2021/7/28.
//
#include <unordered_set>
#include <iostream>

using namespace std;

// Definition for singly-linked list.
struct ListNode {
    int val;
    ListNode *next;

    ListNode(int x) : val(x), next(nullptr) {}
};

class Solution {
public:
    ListNode *removeDuplicateNodes(ListNode *head) {
        if (head == nullptr) {
            return head;
        }
        unordered_set<int> occurred{head->val};
        auto pos = head;
        // 枚举前驱节点
        while (pos->next) {
            // 当前待删除节点
            auto cur = pos->next;
            if (!occurred.count(cur->val)){
                occurred.insert(cur->val);
                pos = pos->next;
            } else{
                pos->next = pos->next->next;
            }
        }
        return head;
    }
};

int main() {

}